<?php

namespace app\controller;

use app\BaseController;
use app\model\User;
use app\model\Role;
use app\validator\UserAssignRoles;
use think\Request;

class UserRoleController extends BaseController{
	public function assign_roles(Request $request){
        $data = $request->param();

        //  验证参数
        $validate = new UserAssignRoles();
        if (!$validate->check($data)) {
            return json([
                'code' => 400,
                'msg' => $validate->getError()
            ], 400);
        }

        $user_id = $data['user_id'];
        $role_id = $data['role_id'];
        $user = User::find($user_id);
        if (!$user)
            return json([
                'code' => 404,
                'msg' => '添加失败'
            ],404);
        $userRoleIds = $user->roles()->column('role_id');
        $newRoleIds = array_diff($role_id, $userRoleIds);
        $result = $user->roles()->attach($newRoleIds);
        if (!$result)
            return json([
                'code' => 400,
                'msg' => '添加失败'
            ],400);
        return json([
            'code' => 200,
            'msg' => '添加成功'
        ]);
    }

    public function unassign_roles(Request $request){
        $data = $request->param();

        //  验证参数
        $validate = new UserAssignRoles();
        if (!$validate->check($data)) {
            return json([
                'code' => 400,
                'msg' => $validate->getError()
            ], 400);
        }

        $user_id = $data['user_id'];
        $role_id = $data['role_id'];
        $user = User::find($user_id);
        if ($user_id === 1 || !$user)
            return json([
                'code' => 404,
                'msg' => '删除失败'
            ],404);
        $result = $user->roles()->detach($role_id);
        if (!$result)
            return json([
                'code' => 400,
                'msg' => '删除失败'
            ]);
        return json([
            'code' => 200,
            'msg' => '删除成功'
        ]);
    }

    public function roles(Request $request){
        $user_id = $request->param('user_id');
        $user = User::find($user_id);
        if (!$user)
            return json([
                'code' => 404,
                'msg' => '获取失败'
            ],404);
        $roles['id'] = $user->roles->column('id');
        $roles['name'] = $user->roles->column('name');
        $roles['description'] = $user->roles->column('description');
        if (!$roles)
            return json([
                'code' => 400,
                'msg' => '获取失败'
            ],400);
        return json([
            'code' => 200,
            'msg' => '获取成功',
            'data' => $roles
        ]);
    }

    public function users(Request $request){
        $role_id = $request->param('role_id');
        $role = Role::find($role_id);
        if (!$role_id)
            return json([
                'code' => 404,
                'msg' => '获取失败'
            ],404);
        $users['name'] = $role->roles->column('name');
        $users['id'] = $role->roles->column('id');
        if (!$users)
            return json([
                'code' => 400,
                'msg' => '获取失败'
            ],400);
        return json([
            'code' => 200,
            'msg' => '获取成功',
            'data' => $users
        ]);
    }
}